package com.tencent.bugly.crashreport.crash;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.os.Environment;
import android.text.TextUtils;
import com.tencent.bugly.BuglyStrategy;
import com.tencent.bugly.crashreport.common.info.PlugInBean;
import com.tencent.bugly.crashreport.common.strategy.StrategyBean;
import com.tencent.bugly.proguard.ad;
import com.tencent.bugly.proguard.ae;
import com.tencent.bugly.proguard.ag;
import com.tencent.bugly.proguard.ah;
import com.tencent.bugly.proguard.aj;
import com.tencent.bugly.proguard.ak;
import com.tencent.bugly.proguard.an;
import com.tencent.bugly.proguard.aq;
import com.tencent.bugly.proguard.bc;
import com.tencent.bugly.proguard.be;
import com.tencent.bugly.proguard.bf;
import com.tencent.bugly.proguard.bg;
import com.tencent.bugly.proguard.bh;
import com.tencent.bugly.proguard.bi;
import com.tencent.bugly.proguard.m;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;

/* compiled from: BUGLY */
/* loaded from: classes.dex */
public class b {
    public static int a;
    protected final Context b;
    protected final ak c;
    protected final ae d;
    protected final com.tencent.bugly.crashreport.common.strategy.a e;
    protected f f;
    protected BuglyStrategy.a g;

    public b(int i, Context context, ak akVar, ae aeVar, com.tencent.bugly.crashreport.common.strategy.a aVar, BuglyStrategy.a aVar2, f fVar) {
        a = i;
        this.b = context;
        this.c = akVar;
        this.d = aeVar;
        this.e = aVar;
        this.g = aVar2;
        this.f = fVar;
    }

    protected List<a> a(List<a> list) {
        if (list == null || list.size() == 0) {
            return null;
        }
        long currentTimeMillis = System.currentTimeMillis();
        ArrayList arrayList = new ArrayList();
        for (a aVar : list) {
            if (aVar.d && aVar.b <= currentTimeMillis - 86400000) {
                arrayList.add(aVar);
            }
        }
        return arrayList;
    }

    protected CrashDetailBean a(List<a> list, CrashDetailBean crashDetailBean) {
        List<CrashDetailBean> b;
        String[] split;
        if (list == null || list.size() == 0) {
            return crashDetailBean;
        }
        CrashDetailBean crashDetailBean2 = null;
        ArrayList arrayList = new ArrayList(10);
        for (a aVar : list) {
            if (aVar.e) {
                arrayList.add(aVar);
            }
        }
        if (arrayList.size() > 0 && (b = b(arrayList)) != null && b.size() > 0) {
            Collections.sort(b);
            CrashDetailBean crashDetailBean3 = null;
            for (int i = 0; i < b.size(); i++) {
                CrashDetailBean crashDetailBean4 = b.get(i);
                if (i == 0) {
                    crashDetailBean3 = crashDetailBean4;
                } else if (crashDetailBean4.s != null && (split = crashDetailBean4.s.split("\n")) != null) {
                    for (String str : split) {
                        if (!crashDetailBean3.s.contains("" + str)) {
                            crashDetailBean3.t++;
                            crashDetailBean3.s += str + "\n";
                        }
                    }
                }
            }
            crashDetailBean2 = crashDetailBean3;
        }
        if (crashDetailBean2 == null) {
            crashDetailBean.j = true;
            crashDetailBean.t = 0;
            crashDetailBean.s = "";
            crashDetailBean2 = crashDetailBean;
        }
        for (a aVar2 : list) {
            if (!aVar2.e && !aVar2.d) {
                if (!crashDetailBean2.s.contains("" + aVar2.b)) {
                    crashDetailBean2.t++;
                    crashDetailBean2.s += aVar2.b + "\n";
                }
            }
        }
        if (crashDetailBean2.r != crashDetailBean.r) {
            if (!crashDetailBean2.s.contains("" + crashDetailBean.r)) {
                crashDetailBean2.t++;
                crashDetailBean2.s += crashDetailBean.r + "\n";
            }
        }
        return crashDetailBean2;
    }

    public boolean a(CrashDetailBean crashDetailBean) {
        return a(crashDetailBean, -123456789);
    }

    public boolean a(CrashDetailBean crashDetailBean, int i) {
        if (crashDetailBean == null) {
            return true;
        }
        if (c.n != null && !c.n.isEmpty()) {
            an.c("Crash filter for crash stack is: %s", c.n);
            if (crashDetailBean.q.contains(c.n)) {
                an.d("This crash contains the filter string set. It will not be record and upload.", new Object[0]);
                return true;
            }
        }
        if (c.o != null && !c.o.isEmpty()) {
            an.c("Crash regular filter for crash stack is: %s", c.o);
            if (Pattern.compile(c.o).matcher(crashDetailBean.q).find()) {
                an.d("This crash matches the regular filter string set. It will not be record and upload.", new Object[0]);
                return true;
            }
        }
        boolean z = crashDetailBean.b == 1;
        String str = crashDetailBean.n;
        String str2 = crashDetailBean.o;
        String str3 = crashDetailBean.p;
        String str4 = crashDetailBean.q;
        long j = crashDetailBean.r;
        String str5 = crashDetailBean.m;
        String str6 = crashDetailBean.e;
        String str7 = crashDetailBean.c;
        String str8 = crashDetailBean.A;
        String str9 = crashDetailBean.B;
        if (this.f != null) {
            an.c("Calling 'onCrashSaving' of RQD crash listener.", new Object[0]);
            if (!this.f.a(z, str, str2, str3, str4, i, j, str5, str6, str7, str8, str9)) {
                an.d("Crash listener 'onCrashSaving' return 'false' thus will not handle this crash.", new Object[0]);
                return true;
            }
        }
        if (crashDetailBean.b != 2) {
            ag agVar = new ag();
            agVar.b = 1;
            agVar.c = crashDetailBean.A;
            agVar.d = crashDetailBean.B;
            agVar.e = crashDetailBean.r;
            this.d.b(1);
            this.d.a(agVar);
            an.b("[crash] a crash occur, handling...", new Object[0]);
        } else {
            an.b("[crash] a caught exception occur, handling...", new Object[0]);
        }
        List<a> b = b();
        List<a> list = null;
        if (b != null && b.size() > 0) {
            list = new ArrayList<>(10);
            List<a> arrayList = new ArrayList<>(10);
            list.addAll(a(b));
            b.removeAll(list);
            if (!com.tencent.bugly.b.c && c.d) {
                boolean z2 = false;
                for (a aVar : b) {
                    if (crashDetailBean.u.equals(aVar.c)) {
                        if (aVar.e) {
                            z2 = true;
                        }
                        arrayList.add(aVar);
                    }
                }
                if (z2 || arrayList.size() >= c.c) {
                    an.a("same crash occur too much do merged!", new Object[0]);
                    CrashDetailBean a2 = a(arrayList, crashDetailBean);
                    for (a aVar2 : arrayList) {
                        if (aVar2.a != a2.a) {
                            list.add(aVar2);
                        }
                    }
                    e(a2);
                    c(list);
                    an.b("[crash] save crash success. For this device crash many times, it will not upload crashes immediately", new Object[0]);
                    return true;
                }
            }
        }
        e(crashDetailBean);
        if (list != null && !list.isEmpty()) {
            c(list);
        }
        an.b("[crash] save crash success", new Object[0]);
        return false;
    }

    public List<CrashDetailBean> a() {
        StrategyBean c = com.tencent.bugly.crashreport.common.strategy.a.a().c();
        if (c == null) {
            an.d("have not synced remote!", new Object[0]);
            return null;
        } else if (!c.g) {
            an.d("Crashreport remote closed, please check your APP ID correct and Version available, then uninstall and reinstall your app.", new Object[0]);
            an.b("[init] WARNING! Crashreport closed by server, please check your APP ID correct and Version available, then uninstall and reinstall your app.", new Object[0]);
            return null;
        } else {
            long currentTimeMillis = System.currentTimeMillis();
            long b = aq.b();
            List<a> b2 = b();
            an.c("Size of crash list loaded from DB: %s", Integer.valueOf(b2.size()));
            if (b2 == null || b2.size() <= 0) {
                return null;
            }
            List<a> arrayList = new ArrayList<>();
            arrayList.addAll(a(b2));
            b2.removeAll(arrayList);
            Iterator<a> it = b2.iterator();
            while (it.hasNext()) {
                a next = it.next();
                if (next.b < b - c.g) {
                    it.remove();
                    arrayList.add(next);
                } else if (next.d) {
                    if (next.b >= currentTimeMillis - 86400000) {
                        it.remove();
                    } else if (!next.e) {
                        it.remove();
                        arrayList.add(next);
                    }
                } else if (next.f >= 3 && next.b < currentTimeMillis - 86400000) {
                    it.remove();
                    arrayList.add(next);
                }
            }
            if (arrayList.size() > 0) {
                c(arrayList);
            }
            List<CrashDetailBean> arrayList2 = new ArrayList<>();
            List<CrashDetailBean> b3 = b(b2);
            if (b3 != null && b3.size() > 0) {
                String str = com.tencent.bugly.crashreport.common.info.a.b().p;
                Iterator<CrashDetailBean> it2 = b3.iterator();
                while (it2.hasNext()) {
                    CrashDetailBean next2 = it2.next();
                    if (!str.equals(next2.f)) {
                        it2.remove();
                        arrayList2.add(next2);
                    }
                }
            }
            if (arrayList2.size() > 0) {
                d(arrayList2);
            }
            return b3;
        }
    }

    public void b(CrashDetailBean crashDetailBean) {
        if (this.f != null) {
            an.c("Calling 'onCrashHandleEnd' of RQD crash listener.", new Object[0]);
            this.f.b(crashDetailBean.b == 1);
        }
    }

    public void a(CrashDetailBean crashDetailBean, long j, boolean z) {
        if (c.l) {
            an.a("try to upload right now", new Object[0]);
            ArrayList arrayList = new ArrayList();
            arrayList.add(crashDetailBean);
            a(arrayList, j, z, crashDetailBean.b == 7, z);
        }
    }

    public void a(final List<CrashDetailBean> list, long j, boolean z, boolean z2, boolean z3) {
        ak akVar;
        if (com.tencent.bugly.crashreport.common.info.a.a(this.b).h && (akVar = this.c) != null) {
            if (z3 || akVar.b(c.a)) {
                StrategyBean c = this.e.c();
                if (!c.g) {
                    an.d("remote report is disable!", new Object[0]);
                    an.b("[crash] server closed bugly in this app. please check your appid if is correct, and re-install it", new Object[0]);
                } else if (list == null || list.size() == 0) {
                } else {
                    try {
                        String str = this.c.b ? c.s : c.t;
                        String str2 = this.c.b ? StrategyBean.c : StrategyBean.a;
                        int i = this.c.b ? 830 : 630;
                        bg a2 = a(this.b, list, com.tencent.bugly.crashreport.common.info.a.b());
                        if (a2 == null) {
                            an.d("create eupPkg fail!", new Object[0]);
                            return;
                        }
                        byte[] a3 = ah.a((m) a2);
                        if (a3 == null) {
                            an.d("send encode fail!", new Object[0]);
                            return;
                        }
                        bh a4 = ah.a(this.b, i, a3);
                        if (a4 == null) {
                            an.d("request package is null.", new Object[0]);
                            return;
                        }
                        aj ajVar = new aj() { // from class: com.tencent.bugly.crashreport.crash.b.1
                            @Override // com.tencent.bugly.proguard.aj
                            public void a(int i2) {
                            }

                            @Override // com.tencent.bugly.proguard.aj
                            public void a(int i2, bi biVar, long j2, long j3, boolean z4, String str3) {
                                b.this.a(z4, list);
                            }
                        };
                        if (z) {
                            this.c.a(a, a4, str, str2, ajVar, j, z2);
                        } else {
                            this.c.a(a, a4, str, str2, ajVar, false);
                        }
                    } catch (Throwable th) {
                        an.e("req cr error %s", th.toString());
                        if (an.b(th)) {
                            return;
                        }
                        th.printStackTrace();
                    }
                }
            }
        }
    }

    public void a(boolean z, List<CrashDetailBean> list) {
        if (list != null && list.size() > 0) {
            an.c("up finish update state %b", Boolean.valueOf(z));
            for (CrashDetailBean crashDetailBean : list) {
                an.c("pre uid:%s uc:%d re:%b me:%b", crashDetailBean.c, Integer.valueOf(crashDetailBean.l), Boolean.valueOf(crashDetailBean.d), Boolean.valueOf(crashDetailBean.j));
                crashDetailBean.l++;
                crashDetailBean.d = z;
                an.c("set uid:%s uc:%d re:%b me:%b", crashDetailBean.c, Integer.valueOf(crashDetailBean.l), Boolean.valueOf(crashDetailBean.d), Boolean.valueOf(crashDetailBean.j));
            }
            for (CrashDetailBean crashDetailBean2 : list) {
                c.a().a(crashDetailBean2);
            }
            an.c("update state size %d", Integer.valueOf(list.size()));
        }
        if (z) {
            return;
        }
        an.b("[crash] upload fail.", new Object[0]);
    }

    public void c(CrashDetailBean crashDetailBean) {
        int i;
        long j;
        String str;
        String str2;
        Map<String, String> map;
        int i2;
        String str3;
        HashMap hashMap;
        if (crashDetailBean == null) {
            return;
        }
        if (this.g == null && this.f == null) {
            return;
        }
        try {
            an.a("[crash callback] start user's callback:onCrashHandleStart()", new Object[0]);
            switch (crashDetailBean.b) {
                case 0:
                    i = 0;
                    break;
                case 1:
                    i = 2;
                    break;
                case 2:
                    i = 1;
                    break;
                case 3:
                    i = 4;
                    break;
                case 4:
                    i = 3;
                    break;
                case 5:
                    i = 5;
                    break;
                case 6:
                    i = 6;
                    break;
                case 7:
                    i = 7;
                    break;
                default:
                    return;
            }
            boolean z = crashDetailBean.b == 1;
            String str4 = crashDetailBean.n;
            String str5 = crashDetailBean.p;
            String str6 = crashDetailBean.q;
            long j2 = crashDetailBean.r;
            byte[] bArr = null;
            if (this.f != null) {
                an.c("Calling 'onCrashHandleStart' of RQD crash listener.", new Object[0]);
                this.f.a(z);
                an.c("Calling 'getCrashExtraMessage' of RQD crash listener.", new Object[0]);
                j = j2;
                str = str5;
                str2 = str6;
                String b = this.f.b(z, str4, str5, str6, -1234567890, j);
                if (b != null) {
                    hashMap = new HashMap(1);
                    hashMap.put("userData", b);
                } else {
                    hashMap = null;
                }
                map = hashMap;
            } else {
                j = j2;
                str = str5;
                str2 = str6;
                if (this.g != null) {
                    an.c("Calling 'onCrashHandleStart' of Bugly crash listener.", new Object[0]);
                    map = this.g.onCrashHandleStart(i, crashDetailBean.n, crashDetailBean.o, crashDetailBean.q);
                } else {
                    map = null;
                }
            }
            if (map != null && map.size() > 0) {
                crashDetailBean.P = new LinkedHashMap(map.size());
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    if (!aq.a(entry.getKey())) {
                        String key = entry.getKey();
                        if (key.length() > 100) {
                            key = key.substring(0, 100);
                            an.d("setted key length is over limit %d substring to %s", 100, key);
                        }
                        if (!aq.a(entry.getValue()) && entry.getValue().length() > 30000) {
                            str3 = entry.getValue().substring(entry.getValue().length() - BuglyStrategy.a.MAX_USERDATA_VALUE_LENGTH);
                            an.d("setted %s value length is over limit %d substring", key, Integer.valueOf((int) BuglyStrategy.a.MAX_USERDATA_VALUE_LENGTH));
                        } else {
                            str3 = "" + entry.getValue();
                        }
                        crashDetailBean.P.put(key, str3);
                        an.a("add setted key %s value size:%d", key, Integer.valueOf(str3.length()));
                    }
                }
            }
            an.a("[crash callback] start user's callback:onCrashHandleStart2GetExtraDatas()", new Object[0]);
            if (this.f != null) {
                an.c("Calling 'getCrashExtraData' of RQD crash listener.", new Object[0]);
                i2 = BuglyStrategy.a.MAX_USERDATA_VALUE_LENGTH;
                bArr = this.f.a(z, str4, str, str2, -1234567890, j);
            } else {
                i2 = BuglyStrategy.a.MAX_USERDATA_VALUE_LENGTH;
                if (this.g != null) {
                    an.c("Calling 'onCrashHandleStart2GetExtraDatas' of Bugly crash listener.", new Object[0]);
                    bArr = this.g.onCrashHandleStart2GetExtraDatas(i, crashDetailBean.n, crashDetailBean.o, crashDetailBean.q);
                }
            }
            byte[] bArr2 = bArr;
            crashDetailBean.U = bArr2;
            if (bArr2 != null) {
                if (bArr2.length > i2) {
                    an.d("extra bytes size %d is over limit %d will drop over part", Integer.valueOf(bArr2.length), Integer.valueOf(i2));
                    crashDetailBean.U = Arrays.copyOf(bArr2, i2);
                }
                an.a("add extra bytes %d ", Integer.valueOf(bArr2.length));
            }
        } catch (Throwable th) {
            an.d("crash handle callback something wrong! %s", th.getClass().getName());
            if (an.a(th)) {
                return;
            }
            th.printStackTrace();
        }
    }

    protected ContentValues d(CrashDetailBean crashDetailBean) {
        if (crashDetailBean == null) {
            return null;
        }
        try {
            ContentValues contentValues = new ContentValues();
            if (crashDetailBean.a > 0) {
                contentValues.put("_id", Long.valueOf(crashDetailBean.a));
            }
            contentValues.put("_tm", Long.valueOf(crashDetailBean.r));
            contentValues.put("_s1", crashDetailBean.u);
            int i = 1;
            contentValues.put("_up", Integer.valueOf(crashDetailBean.d ? 1 : 0));
            if (!crashDetailBean.j) {
                i = 0;
            }
            contentValues.put("_me", Integer.valueOf(i));
            contentValues.put("_uc", Integer.valueOf(crashDetailBean.l));
            contentValues.put("_dt", aq.a(crashDetailBean));
            return contentValues;
        } catch (Throwable th) {
            if (!an.a(th)) {
                th.printStackTrace();
            }
            return null;
        }
    }

    protected CrashDetailBean a(Cursor cursor) {
        if (cursor == null) {
            return null;
        }
        try {
            byte[] blob = cursor.getBlob(cursor.getColumnIndex("_dt"));
            if (blob == null) {
                return null;
            }
            long j = cursor.getLong(cursor.getColumnIndex("_id"));
            CrashDetailBean crashDetailBean = (CrashDetailBean) aq.a(blob, CrashDetailBean.CREATOR);
            if (crashDetailBean != null) {
                crashDetailBean.a = j;
            }
            return crashDetailBean;
        } catch (Throwable th) {
            if (!an.a(th)) {
                th.printStackTrace();
            }
            return null;
        }
    }

    public void e(CrashDetailBean crashDetailBean) {
        if (crashDetailBean == null) {
            return;
        }
        ContentValues d = d(crashDetailBean);
        if (d != null) {
            long a2 = ae.a().a("t_cr", d, (ad) null, true);
            if (a2 >= 0) {
                an.c("insert %s success!", "t_cr");
                crashDetailBean.a = a2;
            }
        }
        if (c.i) {
            f(crashDetailBean);
        }
    }

    public List<CrashDetailBean> b(List<a> list) {
        Cursor cursor;
        if (list == null || list.size() == 0) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        sb.append("_id");
        sb.append(" in ");
        sb.append("(");
        for (a aVar : list) {
            sb.append(aVar.a);
            sb.append(",");
        }
        if (sb.toString().contains(",")) {
            sb = new StringBuilder(sb.substring(0, sb.lastIndexOf(",")));
        }
        sb.append(")");
        String sb2 = sb.toString();
        sb.setLength(0);
        try {
            cursor = ae.a().a("t_cr", null, sb2, null, null, true);
            if (cursor == null) {
                return null;
            }
            try {
                ArrayList arrayList = new ArrayList();
                sb.append("_id");
                sb.append(" in ");
                sb.append("(");
                int i = 0;
                while (cursor.moveToNext()) {
                    try {
                        CrashDetailBean a2 = a(cursor);
                        if (a2 != null) {
                            arrayList.add(a2);
                        } else {
                            sb.append(cursor.getLong(cursor.getColumnIndex("_id")));
                            sb.append(",");
                            i++;
                        }
                    } catch (Throwable th) {
                        th = th;
                        try {
                            if (!an.a(th)) {
                                th.printStackTrace();
                            }
                            if (cursor != null) {
                                cursor.close();
                            }
                            return null;
                        } finally {
                            if (cursor != null) {
                                cursor.close();
                            }
                        }
                    }
                }
                if (sb.toString().contains(",")) {
                    sb = new StringBuilder(sb.substring(0, sb.lastIndexOf(",")));
                }
                sb.append(")");
                String sb3 = sb.toString();
                if (i > 0) {
                    an.d("deleted %s illegal data %d", "t_cr", Integer.valueOf(ae.a().a("t_cr", sb3, (String[]) null, (ad) null, true)));
                }
                if (cursor != null) {
                    cursor.close();
                }
                return arrayList;
            } catch (Throwable th2) {
                th = th2;
            }
        } catch (Throwable th3) {
            th = th3;
            cursor = null;
        }
    }

    protected a b(Cursor cursor) {
        if (cursor == null) {
            return null;
        }
        try {
            a aVar = new a();
            aVar.a = cursor.getLong(cursor.getColumnIndex("_id"));
            aVar.b = cursor.getLong(cursor.getColumnIndex("_tm"));
            aVar.c = cursor.getString(cursor.getColumnIndex("_s1"));
            aVar.d = cursor.getInt(cursor.getColumnIndex("_up")) == 1;
            aVar.e = cursor.getInt(cursor.getColumnIndex("_me")) == 1;
            aVar.f = cursor.getInt(cursor.getColumnIndex("_uc"));
            return aVar;
        } catch (Throwable th) {
            if (!an.a(th)) {
                th.printStackTrace();
            }
            return null;
        }
    }

    public List<a> b() {
        Cursor cursor;
        ArrayList arrayList = new ArrayList();
        try {
            cursor = ae.a().a("t_cr", new String[]{"_id", "_tm", "_s1", "_up", "_me", "_uc"}, null, null, null, true);
            if (cursor == null) {
                return null;
            }
            try {
                StringBuilder sb = new StringBuilder();
                sb.append("_id");
                sb.append(" in ");
                sb.append("(");
                int i = 0;
                while (cursor.moveToNext()) {
                    try {
                        a b = b(cursor);
                        if (b != null) {
                            arrayList.add(b);
                        } else {
                            sb.append(cursor.getLong(cursor.getColumnIndex("_id")));
                            sb.append(",");
                            i++;
                        }
                    } catch (Throwable th) {
                        th = th;
                        try {
                            if (!an.a(th)) {
                                th.printStackTrace();
                            }
                            if (cursor != null) {
                                cursor.close();
                            }
                            return arrayList;
                        } finally {
                            if (cursor != null) {
                                cursor.close();
                            }
                        }
                    }
                }
                StringBuilder sb2 = sb.toString().contains(",") ? new StringBuilder(sb.substring(0, sb.lastIndexOf(","))) : sb;
                sb2.append(")");
                String sb3 = sb2.toString();
                sb2.setLength(0);
                if (i > 0) {
                    an.d("deleted %s illegal data %d", "t_cr", Integer.valueOf(ae.a().a("t_cr", sb3, (String[]) null, (ad) null, true)));
                }
                if (cursor != null) {
                    cursor.close();
                }
                return arrayList;
            } catch (Throwable th2) {
                th = th2;
            }
        } catch (Throwable th3) {
            th = th3;
            cursor = null;
        }
    }

    public void c(List<a> list) {
        if (list == null || list.size() == 0) {
            return;
        }
        StringBuilder sb = new StringBuilder();
        sb.append("_id");
        sb.append(" in ");
        sb.append("(");
        for (a aVar : list) {
            sb.append(aVar.a);
            sb.append(",");
        }
        StringBuilder sb2 = new StringBuilder(sb.substring(0, sb.lastIndexOf(",")));
        sb2.append(")");
        String sb3 = sb2.toString();
        sb2.setLength(0);
        try {
            an.c("deleted %s data %d", "t_cr", Integer.valueOf(ae.a().a("t_cr", sb3, (String[]) null, (ad) null, true)));
        } catch (Throwable th) {
            if (an.a(th)) {
                return;
            }
            th.printStackTrace();
        }
    }

    public void d(List<CrashDetailBean> list) {
        if (list != null) {
            try {
                if (list.size() == 0) {
                    return;
                }
                StringBuilder sb = new StringBuilder();
                for (CrashDetailBean crashDetailBean : list) {
                    sb.append(" or ");
                    sb.append("_id");
                    sb.append(" = ");
                    sb.append(crashDetailBean.a);
                }
                String sb2 = sb.toString();
                if (sb2.length() > 0) {
                    sb2 = sb2.substring(4);
                }
                sb.setLength(0);
                an.c("deleted %s data %d", "t_cr", Integer.valueOf(ae.a().a("t_cr", sb2, (String[]) null, (ad) null, true)));
            } catch (Throwable th) {
                if (an.a(th)) {
                    return;
                }
                th.printStackTrace();
            }
        }
    }

    public static bf a(Context context, CrashDetailBean crashDetailBean, com.tencent.bugly.crashreport.common.info.a aVar) {
        be a2;
        be a3;
        be beVar;
        if (context == null || crashDetailBean == null || aVar == null) {
            an.d("enExp args == null", new Object[0]);
            return null;
        }
        bf bfVar = new bf();
        switch (crashDetailBean.b) {
            case 0:
                bfVar.a = crashDetailBean.j ? "200" : "100";
                break;
            case 1:
                bfVar.a = crashDetailBean.j ? "201" : "101";
                break;
            case 2:
                bfVar.a = crashDetailBean.j ? "202" : "102";
                break;
            case 3:
                bfVar.a = crashDetailBean.j ? "203" : "103";
                break;
            case 4:
                bfVar.a = crashDetailBean.j ? "204" : "104";
                break;
            case 5:
                bfVar.a = crashDetailBean.j ? "207" : "107";
                break;
            case 6:
                bfVar.a = crashDetailBean.j ? "206" : "106";
                break;
            case 7:
                bfVar.a = crashDetailBean.j ? "208" : "108";
                break;
            default:
                an.e("crash type error! %d", Integer.valueOf(crashDetailBean.b));
                break;
        }
        bfVar.b = crashDetailBean.r;
        bfVar.c = crashDetailBean.n;
        bfVar.d = crashDetailBean.o;
        bfVar.e = crashDetailBean.p;
        bfVar.g = crashDetailBean.q;
        bfVar.h = crashDetailBean.z;
        bfVar.i = crashDetailBean.c;
        bfVar.j = null;
        bfVar.l = crashDetailBean.m;
        bfVar.m = crashDetailBean.e;
        bfVar.f = crashDetailBean.B;
        bfVar.t = com.tencent.bugly.crashreport.common.info.a.b().i();
        bfVar.n = null;
        if (crashDetailBean.i != null && crashDetailBean.i.size() > 0) {
            bfVar.o = new ArrayList<>();
            for (Map.Entry<String, PlugInBean> entry : crashDetailBean.i.entrySet()) {
                bc bcVar = new bc();
                bcVar.a = entry.getValue().a;
                bcVar.c = entry.getValue().c;
                bcVar.e = entry.getValue().b;
                bcVar.b = aVar.r();
                bfVar.o.add(bcVar);
            }
        }
        if (crashDetailBean.h != null && crashDetailBean.h.size() > 0) {
            bfVar.p = new ArrayList<>();
            for (Map.Entry<String, PlugInBean> entry2 : crashDetailBean.h.entrySet()) {
                bc bcVar2 = new bc();
                bcVar2.a = entry2.getValue().a;
                bcVar2.c = entry2.getValue().c;
                bcVar2.e = entry2.getValue().b;
                bfVar.p.add(bcVar2);
            }
        }
        if (crashDetailBean.j) {
            bfVar.k = crashDetailBean.t;
            if (crashDetailBean.s != null && crashDetailBean.s.length() > 0) {
                if (bfVar.q == null) {
                    bfVar.q = new ArrayList<>();
                }
                try {
                    bfVar.q.add(new be((byte) 1, "alltimes.txt", crashDetailBean.s.getBytes("utf-8")));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                    bfVar.q = null;
                }
            }
            Object[] objArr = new Object[2];
            objArr[0] = Integer.valueOf(bfVar.k);
            objArr[1] = Integer.valueOf(bfVar.q != null ? bfVar.q.size() : 0);
            an.c("crashcount:%d sz:%d", objArr);
        }
        if (crashDetailBean.w != null) {
            if (bfVar.q == null) {
                bfVar.q = new ArrayList<>();
            }
            try {
                bfVar.q.add(new be((byte) 1, "log.txt", crashDetailBean.w.getBytes("utf-8")));
            } catch (UnsupportedEncodingException e2) {
                e2.printStackTrace();
                bfVar.q = null;
            }
        }
        if (crashDetailBean.x != null) {
            if (bfVar.q == null) {
                bfVar.q = new ArrayList<>();
            }
            try {
                bfVar.q.add(new be((byte) 1, "jniLog.txt", crashDetailBean.x.getBytes("utf-8")));
            } catch (UnsupportedEncodingException e3) {
                e3.printStackTrace();
                bfVar.q = null;
            }
        }
        if (!aq.a(crashDetailBean.V)) {
            if (bfVar.q == null) {
                bfVar.q = new ArrayList<>();
            }
            try {
                beVar = new be((byte) 1, "crashInfos.txt", crashDetailBean.V.getBytes("utf-8"));
            } catch (UnsupportedEncodingException e4) {
                e4.printStackTrace();
                beVar = null;
            }
            if (beVar != null) {
                an.c("attach crash infos", new Object[0]);
                bfVar.q.add(beVar);
            }
        }
        if (crashDetailBean.W != null) {
            if (bfVar.q == null) {
                bfVar.q = new ArrayList<>();
            }
            be a4 = a("backupRecord.zip", context, crashDetailBean.W);
            if (a4 != null) {
                an.c("attach backup record", new Object[0]);
                bfVar.q.add(a4);
            }
        }
        if (crashDetailBean.y != null && crashDetailBean.y.length > 0) {
            be beVar2 = new be((byte) 2, "buglylog.zip", crashDetailBean.y);
            an.c("attach user log", new Object[0]);
            if (bfVar.q == null) {
                bfVar.q = new ArrayList<>();
            }
            bfVar.q.add(beVar2);
        }
        if (crashDetailBean.b == 3) {
            if (bfVar.q == null) {
                bfVar.q = new ArrayList<>();
            }
            an.c("crashBean.userDatas:%s", crashDetailBean.P);
            if (crashDetailBean.P != null && crashDetailBean.P.containsKey("BUGLY_CR_01")) {
                try {
                    if (!TextUtils.isEmpty(crashDetailBean.P.get("BUGLY_CR_01"))) {
                        bfVar.q.add(new be((byte) 1, "anrMessage.txt", crashDetailBean.P.get("BUGLY_CR_01").getBytes("utf-8")));
                        an.c("attach anr message", new Object[0]);
                    }
                } catch (UnsupportedEncodingException e5) {
                    e5.printStackTrace();
                    bfVar.q = null;
                }
                crashDetailBean.P.remove("BUGLY_CR_01");
            }
            if (crashDetailBean.v != null && (a3 = a("trace.zip", context, crashDetailBean.v)) != null) {
                an.c("attach traces", new Object[0]);
                bfVar.q.add(a3);
            }
        }
        if (crashDetailBean.b == 1) {
            if (bfVar.q == null) {
                bfVar.q = new ArrayList<>();
            }
            if (crashDetailBean.v != null && (a2 = a("tomb.zip", context, crashDetailBean.v)) != null) {
                an.c("attach tombs", new Object[0]);
                bfVar.q.add(a2);
            }
        }
        if (aVar.K != null && !aVar.K.isEmpty()) {
            if (bfVar.q == null) {
                bfVar.q = new ArrayList<>();
            }
            StringBuilder sb = new StringBuilder();
            for (String str : aVar.K) {
                sb.append(str);
            }
            try {
                bfVar.q.add(new be((byte) 1, "martianlog.txt", sb.toString().getBytes("utf-8")));
                an.c("attach pageTracingList", new Object[0]);
            } catch (UnsupportedEncodingException e6) {
                e6.printStackTrace();
            }
        }
        if (crashDetailBean.U != null && crashDetailBean.U.length > 0) {
            if (bfVar.q == null) {
                bfVar.q = new ArrayList<>();
            }
            bfVar.q.add(new be((byte) 1, "userExtraByteData", crashDetailBean.U));
            an.c("attach extraData", new Object[0]);
        }
        bfVar.r = new HashMap();
        bfVar.r.put("A9", "" + crashDetailBean.C);
        bfVar.r.put("A11", "" + crashDetailBean.D);
        bfVar.r.put("A10", "" + crashDetailBean.E);
        bfVar.r.put("A23", "" + crashDetailBean.f);
        bfVar.r.put("A7", "" + aVar.l);
        bfVar.r.put("A6", "" + aVar.s());
        bfVar.r.put("A5", "" + aVar.r());
        bfVar.r.put("A22", "" + aVar.h());
        bfVar.r.put("A2", "" + crashDetailBean.G);
        bfVar.r.put("A1", "" + crashDetailBean.F);
        bfVar.r.put("A24", "" + aVar.n);
        bfVar.r.put("A17", "" + crashDetailBean.H);
        bfVar.r.put("A3", "" + aVar.k());
        bfVar.r.put("A16", "" + aVar.m());
        bfVar.r.put("A25", "" + aVar.n());
        bfVar.r.put("A14", "" + aVar.l());
        bfVar.r.put("A15", "" + aVar.w());
        bfVar.r.put("A13", "" + aVar.x());
        bfVar.r.put("A34", "" + crashDetailBean.A);
        if (aVar.D != null) {
            bfVar.r.put("productIdentify", "" + aVar.D);
        }
        try {
            bfVar.r.put("A26", "" + URLEncoder.encode(crashDetailBean.I, "utf-8"));
        } catch (UnsupportedEncodingException e7) {
            e7.printStackTrace();
        }
        if (crashDetailBean.b == 1) {
            bfVar.r.put("A27", "" + crashDetailBean.L);
            bfVar.r.put("A28", "" + crashDetailBean.K);
            bfVar.r.put("A29", "" + crashDetailBean.k);
        }
        bfVar.r.put("A30", "" + crashDetailBean.M);
        bfVar.r.put("A18", "" + crashDetailBean.N);
        Map<String, String> map = bfVar.r;
        StringBuilder sb2 = new StringBuilder();
        sb2.append("");
        sb2.append(!crashDetailBean.O);
        map.put("A36", sb2.toString());
        bfVar.r.put("F02", "" + aVar.w);
        bfVar.r.put("F03", "" + aVar.x);
        bfVar.r.put("F04", "" + aVar.e());
        bfVar.r.put("F05", "" + aVar.y);
        bfVar.r.put("F06", "" + aVar.v);
        bfVar.r.put("F08", "" + aVar.B);
        bfVar.r.put("F09", "" + aVar.C);
        bfVar.r.put("F10", "" + aVar.z);
        if (crashDetailBean.Q >= 0) {
            bfVar.r.put("C01", "" + crashDetailBean.Q);
        }
        if (crashDetailBean.R >= 0) {
            bfVar.r.put("C02", "" + crashDetailBean.R);
        }
        if (crashDetailBean.S != null && crashDetailBean.S.size() > 0) {
            for (Map.Entry<String, String> entry3 : crashDetailBean.S.entrySet()) {
                bfVar.r.put("C03_" + entry3.getKey(), entry3.getValue());
            }
        }
        if (crashDetailBean.T != null && crashDetailBean.T.size() > 0) {
            for (Map.Entry<String, String> entry4 : crashDetailBean.T.entrySet()) {
                bfVar.r.put("C04_" + entry4.getKey(), entry4.getValue());
            }
        }
        bfVar.s = null;
        if (crashDetailBean.P != null && crashDetailBean.P.size() > 0) {
            bfVar.s = crashDetailBean.P;
            an.a("setted message size %d", Integer.valueOf(bfVar.s.size()));
        }
        Object[] objArr2 = new Object[12];
        objArr2[0] = crashDetailBean.n;
        objArr2[1] = crashDetailBean.c;
        objArr2[2] = aVar.e();
        objArr2[3] = Long.valueOf((crashDetailBean.r - crashDetailBean.N) / 1000);
        objArr2[4] = Boolean.valueOf(crashDetailBean.k);
        objArr2[5] = Boolean.valueOf(crashDetailBean.O);
        objArr2[6] = Boolean.valueOf(crashDetailBean.j);
        objArr2[7] = Boolean.valueOf(crashDetailBean.b == 1);
        objArr2[8] = Integer.valueOf(crashDetailBean.t);
        objArr2[9] = crashDetailBean.s;
        objArr2[10] = Boolean.valueOf(crashDetailBean.d);
        objArr2[11] = Integer.valueOf(bfVar.r.size());
        an.c("%s rid:%s sess:%s ls:%ds isR:%b isF:%b isM:%b isN:%b mc:%d ,%s ,isUp:%b ,vm:%d", objArr2);
        return bfVar;
    }

    public static bg a(Context context, List<CrashDetailBean> list, com.tencent.bugly.crashreport.common.info.a aVar) {
        if (context == null || list == null || list.size() == 0 || aVar == null) {
            an.d("enEXPPkg args == null!", new Object[0]);
            return null;
        }
        bg bgVar = new bg();
        bgVar.a = new ArrayList<>();
        for (CrashDetailBean crashDetailBean : list) {
            bgVar.a.add(a(context, crashDetailBean, aVar));
        }
        return bgVar;
    }

    public static be a(String str, Context context, String str2) {
        FileInputStream fileInputStream;
        if (str2 == null || context == null) {
            an.d("rqdp{  createZipAttachment sourcePath == null || context == null ,pls check}", new Object[0]);
            return null;
        }
        an.c("zip %s", str2);
        File file = new File(str2);
        File file2 = new File(context.getCacheDir(), str);
        if (!aq.a(file, file2, 5000)) {
            an.d("zip fail!", new Object[0]);
            return null;
        }
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            fileInputStream = new FileInputStream(file2);
        } catch (Throwable th) {
            th = th;
            fileInputStream = null;
        }
        try {
            byte[] bArr = new byte[4096];
            while (true) {
                int read = fileInputStream.read(bArr);
                if (read <= 0) {
                    break;
                }
                byteArrayOutputStream.write(bArr, 0, read);
                byteArrayOutputStream.flush();
            }
            byte[] byteArray = byteArrayOutputStream.toByteArray();
            an.c("read bytes :%d", Integer.valueOf(byteArray.length));
            be beVar = new be((byte) 2, file2.getName(), byteArray);
            try {
                fileInputStream.close();
            } catch (IOException e) {
                if (!an.a(e)) {
                    e.printStackTrace();
                }
            }
            if (file2.exists()) {
                an.c("del tmp", new Object[0]);
                file2.delete();
            }
            return beVar;
        } catch (Throwable th2) {
            th = th2;
            try {
                if (!an.a(th)) {
                    th.printStackTrace();
                }
                if (fileInputStream != null) {
                    try {
                        fileInputStream.close();
                    } catch (IOException e2) {
                        if (!an.a(e2)) {
                            e2.printStackTrace();
                        }
                    }
                }
                if (file2.exists()) {
                    an.c("del tmp", new Object[0]);
                    file2.delete();
                }
                return null;
            } catch (Throwable th3) {
                if (fileInputStream != null) {
                    try {
                        fileInputStream.close();
                    } catch (IOException e3) {
                        if (!an.a(e3)) {
                            e3.printStackTrace();
                        }
                    }
                }
                if (file2.exists()) {
                    an.c("del tmp", new Object[0]);
                    file2.delete();
                }
                throw th3;
            }
        }
    }

    private boolean f(CrashDetailBean crashDetailBean) {
        try {
            an.c("save eup logs", new Object[0]);
            com.tencent.bugly.crashreport.common.info.a b = com.tencent.bugly.crashreport.common.info.a.b();
            String f = b.f();
            String str = b.p;
            String str2 = crashDetailBean.A;
            Locale locale = Locale.US;
            b.getClass();
            String format = String.format(locale, "#--------\npackage:%s\nversion:%s\nsdk:%s\nprocess:%s\ndate:%s\ntype:%s\nmessage:%s\nstack:\n%s\neupID:%s\n", f, str, "3.0.0", str2, aq.a(new Date(crashDetailBean.r)), crashDetailBean.n, crashDetailBean.o, crashDetailBean.q, crashDetailBean.c);
            String str3 = null;
            if (c.j == null) {
                if (Environment.getExternalStorageState().equals("mounted")) {
                    str3 = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Tencent/" + this.b.getPackageName();
                }
            } else {
                File file = new File(c.j);
                if (file.isFile()) {
                    file = file.getParentFile();
                }
                str3 = file.getAbsolutePath();
            }
            aq.a(this.b, str3 + "/euplog.txt", format, c.k);
            return true;
        } catch (Throwable th) {
            an.d("rqdp{  save error} %s", th.toString());
            if (!an.a(th)) {
                th.printStackTrace();
            }
            return false;
        }
    }

    public static void a(String str, String str2, String str3, String str4, String str5, CrashDetailBean crashDetailBean) {
        String str6;
        com.tencent.bugly.crashreport.common.info.a b = com.tencent.bugly.crashreport.common.info.a.b();
        if (b == null) {
            return;
        }
        an.e("#++++++++++Record By Bugly++++++++++#", new Object[0]);
        an.e("# You can use Bugly(http:\\\\bugly.qq.com) to get more Crash Detail!", new Object[0]);
        an.e("# PKG NAME: %s", b.d);
        an.e("# APP VER: %s", b.p);
        an.e("# LAUNCH TIME: %s", aq.a(new Date(com.tencent.bugly.crashreport.common.info.a.b().a)));
        an.e("# CRASH TYPE: %s", str);
        an.e("# CRASH TIME: %s", str2);
        an.e("# CRASH PROCESS: %s", str3);
        an.e("# CRASH THREAD: %s", str4);
        if (crashDetailBean != null) {
            an.e("# REPORT ID: %s", crashDetailBean.c);
            Object[] objArr = new Object[2];
            objArr[0] = b.m;
            objArr[1] = b.x().booleanValue() ? "ROOTED" : "UNROOT";
            an.e("# CRASH DEVICE: %s %s", objArr);
            an.e("# RUNTIME AVAIL RAM:%d ROM:%d SD:%d", Long.valueOf(crashDetailBean.C), Long.valueOf(crashDetailBean.D), Long.valueOf(crashDetailBean.E));
            an.e("# RUNTIME TOTAL RAM:%d ROM:%d SD:%d", Long.valueOf(crashDetailBean.F), Long.valueOf(crashDetailBean.G), Long.valueOf(crashDetailBean.H));
            if (!aq.a(crashDetailBean.L)) {
                an.e("# EXCEPTION FIRED BY %s %s", crashDetailBean.L, crashDetailBean.K);
            } else if (crashDetailBean.b == 3) {
                Object[] objArr2 = new Object[1];
                if (crashDetailBean.P == null) {
                    str6 = "null";
                } else {
                    str6 = "" + crashDetailBean.P.get("BUGLY_CR_01");
                }
                objArr2[0] = str6;
                an.e("# EXCEPTION ANR MESSAGE:\n %s", objArr2);
            }
        }
        if (!aq.a(str5)) {
            an.e("# CRASH STACK: ", new Object[0]);
            an.e(str5, new Object[0]);
        }
        an.e("#++++++++++++++++++++++++++++++++++++++++++#", new Object[0]);
    }
}
